package com.utoronto.syncgallery.utils;

import java.io.File;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.utoronto.syncgallery.GalleryActivity;

/**
 * FileManagement is the class to handle files.
 * 
 * @author Kobe Sun, Daniel
 * 
 */

public class FileManagement {

	/** Rename the file with new file name newname. */
	public static boolean rename(GalleryActivity gallery, File file,
			String newname) {

		boolean result;
		
		/** File does not exist. */
		if (!file.exists()) {
			show_dialog(gallery, "Rename", "File not exists!");
			return false;
		}
		
		if (newname.equals("")) {
			show_dialog(gallery, "Rename", "File name cannot be empty!");
			return false;
		}
		
		/** Return Error if there is invalid input string. */
		for (int i = 0; 
				i < (SyncGalleryConstants.Gallery_RESERVEDCHARS).length(); 
				i++) {
			if (newname.contains
					(SyncGalleryConstants.Gallery_RESERVEDCHARS.substring(i, i+1))) {
				show_dialog(gallery, "Rename",
						"New name contains invalid characters!");
				return false;
			}
		}

		String path = file.getParentFile().getAbsolutePath() + File.separator
				+ newname;

		if (new File(path).exists()) {
			/** A same name file already exists in SyncGallery folder. */
			show_dialog(gallery, "Rename",
					"A file with the same name already exists in the current folder!");
			return false;
		} else {
			/** Rename the file. */
			result = file.renameTo(new File(path));
			if (result == false) {
				show_dialog(gallery, "Rename",	"Rename Fail!");
				return false;
			}
			return result;
		}
	}

	/** Delete selected folder. */
	public static void delete(final GalleryActivity gallery, final File file) {

		/** Set the UI interface. */
		final AlertDialog alertDialog = new AlertDialog.Builder(gallery)
				.create();

		/** The user chooses to delete. */
		final OnClickListener listener1 = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			    String[] children = file.list();
			    delete_helper(gallery, file, children);		        
			    if (!(file.delete())) {
		        	show_dialog(gallery, "Delete", 
							"Deletion failed!");
		        }
				/** Refresh the current directory. */
				gallery.browseTo(file.getParentFile());
			}
		};

		/** The user cancels their operation. */
		final OnClickListener listener2 = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.cancel();
			}
		};

		/** Show the dialog. */
		alertDialog.setTitle("Delete");
		alertDialog.setMessage("Are you sure?");
		alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", listener1);
		alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", listener2);
		alertDialog.show();
	}
	
	/** delete helper function. */
	public static boolean delete_helper(final GalleryActivity gallery, 
			File file, String[] children) {
		// Check if there is any files in the folder, if yes, delete it.
	    for (int i = 0; i < children.length; i++) {
	    	File temp = new File(file, children[i]);
	    	// If the selected folder contains a folder, delete all the files
	    	// in it and itself.
	    	if (temp.isDirectory()) {
	    		delete_helper(gallery, temp, temp.list());
	    	}
	        if (!(temp.delete())) {
	        	show_dialog(gallery, "Delete", 
						"Deletion failed!");
	        	return false;
	        }
	    }
	    return true;
	}

	/**Create a folder in the current directory.*/
	public static boolean create_folder(GalleryActivity gallery, File file, String name) {
		
		if (name.equals("")) {
			show_dialog(gallery, "Create a folder", "File name cannot be empty!");
			return false;
		}
		
		/** Return Error if there is invalid input string. */
		for (int i = 0; 
				i < (SyncGalleryConstants.Gallery_RESERVEDCHARS).length(); 
				i++) {
			if (name.contains
					(SyncGalleryConstants.Gallery_RESERVEDCHARS.substring(i, i+1))) {
				show_dialog(gallery, "Create a folder",
						"New name contains invalid characters!");
				return false;
			}
		}
		
		String path = file.getAbsolutePath() + File.separator + name;
		
		if (new File(path).exists()) {
			show_dialog(gallery, "Create a new folder", "The folder already exists!");
			return false;
		} else {
			new File(path).mkdir();
		}		
		return true;
	}
	
	public static void show_properties(GalleryActivity gallery, File file) {
		String title = "Properties";
		String filename = "File Name: " + file.getName() + "\n";
		String path = "Path: " + file.getAbsolutePath() + "\n";
		String msg = filename + path;
		show_dialog(gallery, title, msg);
	}
	
	/** Helper function to show error messages. */
	public static void show_dialog(GalleryActivity gallery, String title,
			String msg) {
		final AlertDialog alertDialog = new AlertDialog.Builder(gallery)
				.create();
		final OnClickListener listener1 = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.cancel();
			}
		};
		alertDialog.setTitle(title);
		alertDialog.setMessage(msg);
		alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", listener1);
		alertDialog.show();
	}
}
